package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.CardSchemeDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CardScheme;
import com.ruoyi.system.service.ICardSchemeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 方案Controller
 *
 * @author ruoyi
 * @date 2025-01-25
 */
@RestController
@RequestMapping("/system/scheme")
public class CardSchemeController extends BaseController
{
    @Autowired
    private ICardSchemeService cardSchemeService;

    /**
     * 查询方案列表
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:list')")
    @GetMapping("/list")
    public TableDataInfo list(CardScheme cardScheme)
    {
        startPage();
        List<CardScheme> list = cardSchemeService.selectCardSchemeList(cardScheme);
        return getDataTable(list);
    }

    /**
     * 导出方案列表
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:export')")
    @Log(title = "方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CardScheme cardScheme)
    {
        List<CardScheme> list = cardSchemeService.selectCardSchemeList(cardScheme);
        ExcelUtil<CardScheme> util = new ExcelUtil<CardScheme>(CardScheme.class);
        util.exportExcel(response, list, "方案数据");
    }

    /**
     * 获取方案详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:query')")
    @GetMapping(value = "/{schemeId}")
    public AjaxResult getInfo(@PathVariable("schemeId") Long schemeId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", cardSchemeService.selectDeptListBySchemeId(schemeId));
        ajax.put("schemes", cardSchemeService.selectCardSchemeBySchemeId(schemeId));
        return ajax;
    }

    /**
     * 新增方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:add')")
    @Log(title = "方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CardScheme cardScheme)
    {
        if (!cardSchemeService.checkSchemeNameUnique(cardScheme))
        {
            return error("新增方案'" + cardScheme.getSchemeName() + "'失败，方案名称已存在");
        }
        SysUser user = getLoginUser().getUser();
        cardScheme.setUserId(user.getUserId());
        cardScheme.setDeptId(user.getDeptId());
        cardScheme.setCreateBy(user.getUserName());
        return toAjax(cardSchemeService.insertCardScheme(cardScheme));
    }

    /**
     * 修改方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:edit')")
    @Log(title = "方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CardScheme cardScheme)
    {
        SysUser user = getLoginUser().getUser();
        cardScheme.setUserId(user.getUserId());
        cardScheme.setDeptId(user.getDeptId());
        cardScheme.setUpdateBy(user.getUserName());
        return toAjax(cardSchemeService.updateCardScheme(cardScheme));
    }

    /**
     * 删除方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:remove')")
    @Log(title = "方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{schemeIds}")
    public AjaxResult remove(@PathVariable Long[] schemeIds)
    {
        return toAjax(cardSchemeService.deleteCardSchemeBySchemeIds(schemeIds));
    }

    @PostMapping(value = "/getSchemeDept")
    public TableDataInfo getSchemeDept(@RequestBody CardScheme cardScheme)
    {
        startPage();
        List<CardSchemeDept> list = cardSchemeService.getSchemeDept(cardScheme);
        return getDataTable(list);
    }
}
